<template>
	<view class="content b-t">
		<view class="list b-b" v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)">
			<view class="wrapper">
				<view class="address-box">
					<text v-if="item.isDefault" class="tag">默认</text>
					<text class="name m-r-xs">{{item.name}} </text>
					<text v-if="item.gender == 1">先生</text>
					<text v-if="item.gender == 2">女士</text>
					<text class="m-l-xs">{{item.tel}}</text>
				</view>
				<view class="u-box">
					<text class="city">{{item.city}}</text>
					<text class="address">{{item.address}}</text>
				</view>
			</view>
			<text class="xyicon xyicon-edit" @click.stop="addAddress('edit', item)"></text>
		</view>
		<text style="display:block;padding: 16upx 30upx 10upx;lihe-height: 1.6;color: #fa436a;font-size: 24upx;">
			提示：您可以在这里管理您的收货地址
		</text>
		<button class="add-btn" @click="addAddress('add')">新增地址</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				source: 0,
				addressList: []
			}
		},
		onLoad(option){
			console.log(option.source);
			this.source = option.source;
		},
		onShow(){
			this.loadData();
		},
		async onPullDownRefresh() {
			await this.loadData();
			uni.stopPullDownRefresh();
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 5000);
		},
		methods: {
			async loadData() {
				uni.showLoading({
				    title: '加载中'
				});
				let res1 = await this.util.request({
					url: '/v1/user_address/address/my',
					method: 'get'
				});
				if (res1.code == 200) {
					this.addressList = res1.data.dataList;
				} else {
					uni.showToast({
						title: res.msg
					})
				}
				uni.hideLoading();
			},
			// 选择地址
			checkAddress(item){
				if(this.source == 1){
					// 在下单页面需要选择一个收货地址时调用
					// 获取上一页实例，可直接调用上页所有数据和方法，在App.vue定义
					var pages = getCurrentPages();
					var prePage = pages[pages.length - 2];
					//prePage.setAddress(item);
					prePage.$vm.setAddress(item);
					setTimeout(()=>{
						uni.navigateBack();
					}, 800);
				}
			},
			addAddress(type, item){
				uni.navigateTo({
					url: `/pages/user_address/address/manage?type=${type}&data=${JSON.stringify(item)}`
				})
			},
			// 添加或修改成功之后回调
			refreshList(data, type){
				// 添加或修改后刷新地址列表
				this.loadData();
			}
		}
	}
</script>

<style lang='scss'>
	page{
		padding-bottom: 120upx;
	}
	.content{
		position: relative;
	}
	.list{
		display: flex;
		align-items: center;
		padding: 20upx 30upx;;
		background: #fff;
		position: relative;
	}
	.wrapper{
		display: flex;
		flex-direction: column;
		flex: 1;
	}
	.address-box{
		display: flex;
		align-items: center;
		.tag{
			font-size: 24upx;
			color: $uni-color-primary;
			margin-right: 10upx;
			background: #fffafb;
			border: 1px solid #ffb4c7;
			border-radius: 4upx;
			padding: 4upx 10upx;
			line-height: 1;
		}
		.address{
			font-size: 30upx;
			color: $font-color-dark;
		}
	}
	.u-box{
		font-size: 28upx;
		color: $font-color-light;
		margin-top: 16upx;
		.name{
			margin-right: 30upx;
		}
	}
	.icon-bianji{
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 40upx;
		color: $font-color-light;
		padding-left: 30upx;
	}
	.add-btn{
		position: fixed;
		left: 30upx;
		right: 30upx;
		bottom: 16upx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		font-size: 32upx;
		color: #fff;
		background-color: $uni-color-primary;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
